﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MidApp
{
    /// <summary>
    ///Trida spravujici týmy.
    ///</summary>
    public class ManageTeam
    {
        /// <summary>
        ///Metoda vlozi novy tym do databaze.
        ///</summary>
        public static void CreateNewTeam(AppLib.Team newTeam)
        {
            var db = MidApp.LinqUse.DBConn;

            AppLib.Team sum = new AppLib.Team();
            try
            {
                sum = (from Team in db.Teams orderby Team.TeamID descending select Team).First();
            }
            catch (Exception)
            {

            }
            finally
            {
                if (sum == null)
                    newTeam.TeamID = 1;
                else
                    newTeam.TeamID = sum.TeamID + 1;

                db.Teams.InsertOnSubmit(newTeam);
                db.SubmitChanges();
            }

        }
        /// <summary>
        ///Metoda zjistuje, zda tym s danym jmenen jiz exituje.
        ///</summary>
        public static bool TeamNameAlreadyTaken(string name)
        {
            var db = MidApp.LinqUse.DBConn;

            var files = (from Team in db.Teams where Team.TeamName.Equals(name) select Team).ToList();
            
            bool b = true;

            if (files.Count > 0)
                b = true;
            else
                b = false;

            return b;

        }
        /// <summary>
        ///Metoda vraci, ve kterych tymech je uzivatel vedoucim.
        ///</summary>
        public static List<AppLib.Team> GetTeamsWhereUserIsLeader(int userID)
        {
            var db = MidApp.LinqUse.DBConn;

            //return (from Team in db.Teams where Team.LeaderID==userID && Team.Active == true orderby Team.DateOfFoundation ascending select Team).ToList();
            return (from Team in db.Teams join IsMember in db.IsMembers on Team.TeamID equals IsMember.TeamID where IsMember.UserID == userID && Team.Active == true && IsMember.IsLeader==true orderby Team.DateOfFoundation ascending select Team).ToList();
            
        }
        /// <summary>
        ///Metoda vraci, ve kterych tymech je uzivatel clenem.
        ///</summary>
        public static List<AppLib.Team> GetTeamsWhereUserIsMember(int userID)
        {
            var db = MidApp.LinqUse.DBConn;

            return (from Team in db.Teams join IsMember in db.IsMembers on Team.TeamID equals IsMember.TeamID where IsMember.UserID == userID && Team.Active == true && IsMember.IsLeader==false orderby Team.DateOfFoundation ascending select Team).ToList();

        }
        /// <summary>
        ///Metoda vraci, ve kterych tymech je uzivatel clenem. nerozlisuje role.
        ///</summary>
        public static List<AppLib.Team> GetTeamsWhereUserIsAnyMember(int userID)
        {
            var db = MidApp.LinqUse.DBConn;

            return (from Team in db.Teams join IsMember in db.IsMembers on Team.TeamID equals IsMember.TeamID where IsMember.UserID == userID && Team.Active == true orderby Team.DateOfFoundation ascending select Team).ToList();

        }
        /// <summary>
        ///Metoda vraci, ve kterych tymech je uzivatel pokladnikem.
        ///</summary>
        public static List<AppLib.Team> GetTeamsWhereUserIsTreasurer(int userID)
        {
            var db = MidApp.LinqUse.DBConn;

            return (from Team in db.Teams join IsMember in db.IsMembers on Team.TeamID equals IsMember.TeamID where IsMember.UserID == userID && Team.Active == true && IsMember.IsTreasurer == true orderby Team.DateOfFoundation ascending select Team).ToList();

        }

        /// <summary>
        ///Metoda vraci, vsechny tymy.
        ///</summary>
        public static List<AppLib.Team> GetAllActiveTeamsOrderedByTeamName()
        {
            var db = MidApp.LinqUse.DBConn;

            return (from Team in db.Teams where Team.Active == true orderby Team.TeamName ascending select Team).ToList();

        }
        /// <summary>
        ///Metoda vraci tym podle jeho ID.
        ///</summary>
        public static AppLib.Team GetTeamByID (int teamID)
        {
            var db = MidApp.LinqUse.DBConn;

            return (from Team in db.Teams where Team.TeamID == teamID select Team).First();
        }
        /// <summary>
        ///Metoda vraci tym podle jeho jmeno.
        ///</summary>
        public static AppLib.Team GetTeamByTeamName(string teamName)
        {
            var db = MidApp.LinqUse.DBConn;

                return (from Team in db.Teams where Team.TeamName == teamName && Team.Active==true select Team).First();

        }
        /// <summary>
        ///Metoda vraci tym podle zvolene lokace.
        ///</summary>
        public static AppLib.Team GetTeamByLocation(string location)
        {
            var db = MidApp.LinqUse.DBConn;

            return (from Team in db.Teams where Team.Location == location && Team.Active == true select Team).First();
        }
        /// <summary>
        ///Metoda vraci seznam tymu podle jejich zakladatele.
        ///</summary>
        public static List<AppLib.Team> GetTeamsByFounder(int founderID)
        {
            var db = MidApp.LinqUse.DBConn;

            return (from Team in db.Teams where Team.LeaderID == founderID && Team.Active == true select Team).ToList();
        }
        /// <summary>
        ///Metoda vraci tymy, jejich nazev obsahuje prefix
        ///</summary>
        public static List<AppLib.Team> GetTeamListOfTeamsContainingTeamName(string teamName)
        {
            var db = MidApp.LinqUse.DBConn;            

            List<AppLib.Team> teams = GetAllActiveTeamsOrderedByTeamName();
            List<string> teamNames = new List<string>();

            foreach (var team in teams)
            {
                if (team.TeamName.ToLower().Contains(teamName.ToLower()))
                {
                    teamNames.Add(team.TeamName);
                }             
            }
            List<AppLib.Team> pom = new List<AppLib.Team>();
            foreach (var item in teamNames)
            {
                pom.Add(GetTeamByTeamName(item));
            }
            return pom;

        }
        /// <summary>
        ///Metoda vraci tymy, jejich zakladatel obsahuje zadany prefix
        ///</summary>
        public static List<AppLib.Team> GetTeamListOfTeamsContainingFounder(string Founder)
        {
            var db = MidApp.LinqUse.DBConn;

            List<AppLib.Team> teams = GetAllActiveTeamsOrderedByTeamName();
            List<int> users = new List<int>();

            foreach (var team in teams)
            {
                if (MidApp.ManageUser.GetUserByID(team.LeaderID).Nick.ToLower().Contains(Founder.ToLower()))
                {
                    users.Add(MidApp.ManageUser.GetUserByID(team.LeaderID).UserID);
                }
            }

            users = users.Distinct().ToList();

            List<AppLib.Team> pom = new List<AppLib.Team>();
            foreach (var leader in users)
            {
               List<AppLib.Team> pom1=GetTeamsByFounder(leader);
               foreach (var team in pom1)
               {
                   pom.Add(team);
               }
            }
            return pom;          
        }
        /// <summary>
        ///Metoda vraci tymy, jejich lokace obsahuje zadany prefix
        ///</summary>
        public static List<AppLib.Team> GetTeamListOfTeamsContainingLocation(string Location)
        {
            var db = MidApp.LinqUse.DBConn;

            List<AppLib.Team> teams = GetAllActiveTeamsOrderedByTeamName();
            List<string> locations = new List<string>();

            foreach (var team in teams)
            {
                if (team.Location.ToLower().Contains(Location.ToLower()))
                {
                    locations.Add(team.Location);
                }
            }
            List<AppLib.Team> pom = new List<AppLib.Team>();
            foreach (var item in locations)
            {
                pom.Add(GetTeamByLocation(item));
            }
            return pom;            
        }
        /// <summary>
        ///Metoda aktualizuje udaje o teamu v DB.
        /// </summary>
        public static void AssignNewTeamInfo(AppLib.Team UpdatedTeam)
        {
            var db = MidApp.LinqUse.DBConn;
            var oldTeam = (from Team in db.Teams where Team.TeamID == UpdatedTeam.TeamID select Team).First();

            oldTeam.TeamName = UpdatedTeam.TeamName;
            oldTeam.Location = UpdatedTeam.Location;
            oldTeam.ImageURL = UpdatedTeam.ImageURL;
            oldTeam.Description = UpdatedTeam.Description;

            db.SubmitChanges();

        }
        /// <summary>
        ///Metoda deaktivuje tym.
        /// </summary>
        public static void DeactivateTeam(int teamID)
        {
            var db = MidApp.LinqUse.DBConn;
            var teamToDelete = (from Team in db.Teams where Team.TeamID == teamID select Team).First();

            teamToDelete.Active = false;
            db.SubmitChanges();
            
        }
        /// <summary>
        ///Metoda vrati tymy serazene podle jmea.
        /// </summary>
        public static List<AppLib.Team> GetAllActiveTeamsOrderedByLocation()
        {
            var db = MidApp.LinqUse.DBConn;

            return (from Team in db.Teams where Team.Active == true orderby Team.Location ascending select Team).ToList();

        }
        /// <summary>
        ///Metoda vraci vedouci daneho tymu.
        /// </summary>
        public static List<AppLib.Team> GetTeamsLeaders(int TeamID)
        {
            var db = MidApp.LinqUse.DBConn;
            return (from Team in db.Teams join IsMember in db.IsMembers on Team.TeamID equals IsMember.TeamID where IsMember.TeamID == TeamID && Team.Active == true && IsMember.IsLeader == true orderby Team.DateOfFoundation ascending select Team).ToList();
        }

        /// <summary>
        ///Metoda vraci seznam stringu, ktery obsahuje lokace, ktere obsahuji prefix
        /// </summary>
        public static List<string> GetAllTeamLocations(List<AppLib.Team> teams, string prefixText)
        {
            List<string> locations = new List<string>();
            foreach (var team in teams)
	        {
                locations.Add(team.Location);
	        }
            locations = locations.Distinct().ToList();

            List<string> pom = new List<string>(locations);
            foreach (var location in pom)
            {
                if (!location.ToLower().StartsWith(prefixText.ToLower()))
                {
                    locations.Remove(location);
                }
            }

            return locations;
        }
        /// <summary>
        ///Metoda vraci seznam stringu, ktery obsahuje jmena leaderu, kteri obsahuji prefix
        /// </summary>
        public static List<string> GetAllTeamLeaders(List<AppLib.Team> teams, string prefixText)
        {
            List<string> leaders = new List<string>();
            foreach (var team in teams)
            {                
                leaders.Add(MidApp.ManageUser.GetUserByID(team.LeaderID).Nick);
            }
            leaders = leaders.Distinct().ToList();

            List<string> pom = new List<string>(leaders);
            foreach (var leader in pom)
            {
                if (!leader.ToLower().StartsWith(prefixText.ToLower()))
                {
                    leaders.Remove(leader);
                }
            }
            return leaders;
        }
        /// <summary>
        ///Metoda vraci seznam stringu, ktery obsahuje jmena tymu, ktere obsahuji prefix
        /// </summary>
        public static List<string> GetAllTeamNames(List<AppLib.Team> teams, string prefixText)
        {
            List<string> teamNames = new List<string>();
            foreach (var team in teams)
            {
                teamNames.Add(team.TeamName);
            }
            teamNames = teamNames.Distinct().ToList();

            List<string> pom = new List<string>(teamNames);
            foreach (var teamName in pom)
            {
                if (!teamName.ToLower().StartsWith(prefixText.ToLower()))
                {
                    teamNames.Remove(teamName);
                }
            }
            return teamNames;
        }
    }
}
